// mmo_game_server/communication/rabbitmq_client.go
package communication

import (
    "log"

    "github.com/streadway/amqp"
)

// RabbitMQClient 创建RabbitMQ客户端
func RabbitMQClient(url string) (*amqp.Connection, error) {
    conn, err := amqp.Dial(url)
    if err != nil {
        log.Printf("Failed to connect to RabbitMQ: %v", err)
        return nil, err
    }
    return conn, nil
}

// SendMessage 发送消息到RabbitMQ
func SendMessage(conn *amqp.Connection, queueName, message string) error {
    ch, err := conn.Channel()
    if err != nil {
        log.Printf("Failed to open a channel: %v", err)
        return err
    }
    defer ch.Close()

    q, err := ch.QueueDeclare(
        queueName, // name
        false,     // durable
        false,     // delete when unused
        false,     // exclusive
        false,     // no-wait
        nil,       // arguments
    )
    if err != nil {
        log.Printf("Failed to declare a queue: %v", err)
        return err
    }

    err = ch.Publish(
        "",     // exchange
        q.Name, // routing key
        false,  // mandatory
        false,  // immediate
        amqp.Publishing{
            ContentType: "text/plain",
            Body:        []byte(message),
        })
    if err != nil {
        log.Printf("Failed to publish a message: %v", err)
        return err
    }
    return nil
}